/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DBHandling.BillDetailHandling;
import Entity.BillDetail;
import Utilities.MyLibrary;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author HuyPV
 */
public class ValidateInventoryPanel extends javax.swing.JPanel {
    
    DefaultTableModel model;
    Vector<BillDetail> details;
    Date currentDate;
    DefaultListModel listmodel;
    Vector<String> listBill;
    
    public ValidateInventoryPanel() {
        initComponents();
        setOptionTable();
        loadData();
    }
    
    private void setOptionTable() {
        model = new DefaultTableModel() {
            
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        tblBill.setModel(model);
        
        model.addColumn("#");
        model.addColumn("Item");
        model.addColumn("Price");
        model.addColumn("Quantity");
        model.addColumn("Cost");
        
        listmodel = new DefaultListModel();
        currentDate = Calendar.getInstance().getTime();
        currentDate.setDate(currentDate.getDate()-1 );
        listBills.setModel(listmodel);
        listBills.setSelectedIndex(0);
    }

    //load data from database to table 
    private void loadData() {
        try {
            BillDetailHandling bdh = new BillDetailHandling();
            details = new Vector<BillDetail>();
            
            
            SimpleDateFormat df = new SimpleDateFormat("MM/dd/YYYY");
            String date = df.format(currentDate);
            details = bdh.getBillDetails(date);
            lblDate.setText(date);
            listBill = new Vector<String>();
            listBills.removeAll();    //clear all element on list
            for (BillDetail bd : details) {
                String billID =bd.getBill().getBillID();
                if(!checkExistedBillID(billID)){
                    listBill.add(billID);
                }
            }
            for (String bID : listBill) {
                listmodel.addElement(bID);
            }
            
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Notice", JOptionPane.OK_OPTION);
        }
        
    }
    
    private boolean checkExistedBillID(String billID){
        for (String bID : listBill) {
            if(bID.equals(billID)) return true;
        }
        return false;
    }
    
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblBanner = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        lblDate = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lblCustomer = new javax.swing.JLabel();
        lblBillID = new javax.swing.JLabel();
        lblPayment = new javax.swing.JLabel();
        lblStaff = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblBill = new javax.swing.JTable();
        jLabel6 = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listBills = new javax.swing.JList();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setMinimumSize(new java.awt.Dimension(700, 300));
        setPreferredSize(new java.awt.Dimension(747, 591));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblBanner.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/banner2.jpg"))); // NOI18N
        lblBanner.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        lblBanner.setName("lblBanner"); // NOI18N
        add(lblBanner, new org.netbeans.lib.awtextra.AbsoluteConstraints(-10, 0, 1200, 100));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Preview bill", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Handwriting", 0, 14), new java.awt.Color(0, 0, 204))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblDate.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblDate.setForeground(new java.awt.Color(102, 0, 102));
        lblDate.setName("lblDate"); // NOI18N
        jPanel1.add(lblDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 10, 100, 20));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 36)); // NOI18N
        jLabel1.setText("Sale bill");
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 30, 130, 37));

        jSeparator1.setName("jSeparator1"); // NOI18N
        jPanel1.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 80, 250, 10));

        jLabel2.setText("Customer :");
        jLabel2.setName("jLabel2"); // NOI18N
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, -1, 20));

        jLabel3.setText("Bill ID:");
        jLabel3.setName("jLabel3"); // NOI18N
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 130, 30, 20));

        jLabel4.setText("Payment method:");
        jLabel4.setName("jLabel4"); // NOI18N
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 100, -1, 20));

        jLabel5.setText("Sales staff:");
        jLabel5.setName("jLabel5"); // NOI18N
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 130, -1, 20));

        lblCustomer.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblCustomer.setForeground(new java.awt.Color(0, 0, 255));
        lblCustomer.setName("lblCustomer"); // NOI18N
        jPanel1.add(lblCustomer, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 100, 100, 20));

        lblBillID.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblBillID.setForeground(new java.awt.Color(0, 0, 255));
        lblBillID.setName("lblBillID"); // NOI18N
        jPanel1.add(lblBillID, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 130, 80, 20));

        lblPayment.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblPayment.setForeground(new java.awt.Color(0, 0, 255));
        lblPayment.setName("lblPayment"); // NOI18N
        jPanel1.add(lblPayment, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 100, 110, 20));

        lblStaff.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblStaff.setForeground(new java.awt.Color(0, 0, 255));
        lblStaff.setName("lblStaff"); // NOI18N
        jPanel1.add(lblStaff, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 130, 110, 20));

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        tblBill.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblBill.setName("tblBill"); // NOI18N
        jScrollPane2.setViewportView(tblBill);

        jPanel1.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 200, 510, 170));

        jLabel6.setText("Total: ");
        jLabel6.setName("jLabel6"); // NOI18N
        jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 420, -1, -1));

        lblTotal.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblTotal.setForeground(new java.awt.Color(102, 0, 102));
        lblTotal.setText("$");
        lblTotal.setName("lblTotal"); // NOI18N
        jPanel1.add(lblTotal, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 420, 70, -1));

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(0, 0, 255));
        jLabel7.setText("Bill detail");
        jLabel7.setName("jLabel7"); // NOI18N
        jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 180, -1, -1));

        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 160, 530, 440));

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        listBills.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "List of bills", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Handwriting", 0, 14), new java.awt.Color(0, 51, 204))); // NOI18N
        listBills.setName("listBills"); // NOI18N
        listBills.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listBillsValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(listBills);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 160, 240, 440));

        jLabel8.setFont(new java.awt.Font("Lucida Handwriting", 0, 24)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(0, 0, 204));
        jLabel8.setText("Sales done in previous day");
        jLabel8.setName("jLabel8"); // NOI18N
        add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 100, -1, -1));

        jLabel9.setFont(new java.awt.Font("Times New Roman", 0, 18)); // NOI18N
        jLabel9.setForeground(new java.awt.Color(102, 0, 102));
        jLabel9.setText("<html>Please select bill ID from List of bills to preview bill"); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N
        add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 230, 70, 200));
    }// </editor-fold>//GEN-END:initComponents

    private void listBillsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listBillsValueChanged
        // TODO add your handling code here:
        String billId = listBills.getSelectedValue().toString();
        double total=0;
        try {
            //load from list to table
            MyLibrary.clearAllRow(model);      //clear all row on table
            int i = 1;
            for (BillDetail bd : details) {
                if (billId.equals(bd.getBill().getBillID())) {
                    Vector v = new Vector();
                    v.addElement(i++);                    
                    v.addElement(bd.getItemName());
                    v.addElement(bd.getPrice());
                    v.addElement(bd.getQuantity());
                    v.addElement(bd.getPrice() * bd.getQuantity());
                    model.addRow(v);
                    lblBillID.setText(billId);
                    lblCustomer.setText(bd.getBill().getCustomerName());
                    lblPayment.setText(bd.getBill().getPayment());
                    lblStaff.setText(bd.getBill().getUsername());
                    total += bd.getPrice() * bd.getQuantity();
                }
            }
            lblTotal.setText(String.valueOf(total)+ " $");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Notice", JOptionPane.OK_OPTION);
        }
    }//GEN-LAST:event_listBillsValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblBanner;
    private javax.swing.JLabel lblBillID;
    private javax.swing.JLabel lblCustomer;
    private javax.swing.JLabel lblDate;
    private javax.swing.JLabel lblPayment;
    private javax.swing.JLabel lblStaff;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JList listBills;
    private javax.swing.JTable tblBill;
    // End of variables declaration//GEN-END:variables
}
